const http=require("http");
const {URL}=require('url');

// const router=require("./router");

function start(route){
    function onRequest(req,res){
        const pathname=new URL(req.url,`http://${req.headers.host}`).pathname;
        // console.log(req.url);
        // console.log(new URL(req.url,`http://${req.headers.host}`).href);
        console.log(`request for ${pathname} received.`);
        route(pathname);

        res.writeHead(200,{'Content-Type':'text/plain'});
        res.write('Hello world!');
        res.end();
    }

    http.createServer(onRequest).listen(3003);
    console.log("Server started on port 3003");
}

module.exports.start = start;



// const myUrl=new URL('http://localhost:3003/start?foo=bar&hello=world');
//
// console.log(myUrl.pathname);
// console.log(myUrl.searchParams.get('foo'));
// console.log(myUrl.searchParams.get('hello'));
//
// const server=http.createServer((req,res)=>{
//     const {url,method} = req;
//     console.log(req.method);
//     if(url==='/' && method==='GET'){
//         res.writeHead(200,{'Content-Type':'text/plain'});
//         res.end('Home Page');
//     }else if(url==='/about' && method==='GET'){
//         res.writeHead(200,{'Content-Type':'text/plain'});
//         res.end('About Page');
//     }else{
//         res.writeHead(404,{'Content-Type':'text/plain'});
//         res.end('404 Not Found');
//     }
// });

// server.listen(3003,()=>{
//     console.log("Server started on port 3003");
// })
